﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VicMessenger.DAL;
using VicMessenger.DB;
using System.Data;
using System.Reflection;

namespace VicMessenger.BLL
{
    public class ContactBLL
    {
        public static List<proc_Contact_SelectResult> GetContactByID(long prmContactID)
        {
            try
            {
                return ContactDAL.GetContact(prmContactID, null);
            }
            catch (Exception)
            {

                return null;
            }
        }
        public static List<proc_Contact_SelectResult> GetContactByCampaignID(int prmCampaignID)
        {
            try
            {
                return ContactDAL.GetContact(null, prmCampaignID);
            }
            catch (Exception)
            {

                return null;
            }
        }

        public static List<proc_Contact_CustomPagingResult> GetContactCustomPaging(int? prmCampaignID, int? prmPageIndex, int? prmMaxRow, out int? prmTotalRows)
        {
            try
            {
                return ContactDAL.GetContactCustomPaging(prmCampaignID, prmPageIndex, prmMaxRow, out prmTotalRows);
            }
            catch (Exception)
            {
                prmTotalRows = 0;
                return null;
            }
        }


        public static DataTable GetHorizontalContactCustomPaging(int? prmCampaignID, int? prmPageIndex, int? prmMaxRow, out int? prmTotalRows)
        {
            try
            {
                DataTable csvDataTable = new DataTable();

                //column for contact
                foreach (PropertyInfo p in typeof(proc_Contact_CustomPagingResult).GetProperties())
                {
                    csvDataTable.Columns.Add(p.Name, typeof(string));
                }


                  List<proc_Contact_CustomPagingResult> retContacts =  ContactDAL.GetContactCustomPaging(prmCampaignID, prmPageIndex, prmMaxRow, out prmTotalRows);

                if (retContacts != null)
                {
                    
                
                    if (retContacts.Count > 0)
                    {
                        //column for customfield
                        var retCustomFieldTemplate = CustomFieldTemplateBLL.GetCustomFieldTemplate(retContacts[0].CampaignID);
                        foreach (proc_CustomeFieldTemplate_SelectResult item in retCustomFieldTemplate)
                        {
                            csvDataTable.Columns.Add(item.FieldName, typeof(string));
                        }
                    }

                    int fildCount = 0;
                    foreach (proc_Contact_CustomPagingResult item in retContacts)
                    {
                        DataRow dtrow = csvDataTable.NewRow();

                        CreateGridViewColumns<proc_Contact_CustomPagingResult>(item, ref dtrow,  ref fildCount);
                        //foreach (var rowField in fieldCollection)
                        //{
                        //    dtrow[ctr] = rowField;
                        //    ctr = ctr + 1;
                        //}



                        var retCustomField = CustomFieldBLL.GetCustomFieldsByContactID(item.ContactID);
                        foreach (proc_CustomeField_SelectResult itemField in retCustomField)
                        {
                            dtrow[fildCount] = itemField.FieldValue;
                            fildCount = fildCount + 1; 
                        }

                        csvDataTable.Rows.Add(dtrow);
                    }          
                
                }



                return csvDataTable;
            }
            catch (Exception)
            {
                prmTotalRows = 0;
                return null;
            }
        }


        public static void CreateGridViewColumns<T>(object prmMod, ref DataRow prmDataRow, ref int prmCtr)
        {
            int ctr = 0;
            //prmDataRow = null;
            foreach (PropertyInfo p in typeof(T).GetProperties())
            {
                prmDataRow[ctr] = p.GetValue(prmMod, null).ToString();
                ctr = ctr + 1;
            }
            prmCtr = ctr;
        }

        public static long? InsertContact(proc_Contact_CustomPagingResult prmContact)
        {
            try
            {
               return ContactDAL.InsertUpdateContact(prmContact);
            }
            catch (Exception)
            {

                return null;
            }
        }

        public static int DeleteContact(long prmContactId)
        {
            try
            {
                return ContactDAL.DeleteContact(prmContactId);
            }
            catch (Exception)
            {

                return 0;
            }
        }
    }
}
